import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import QtGraphicalEffects 1.14

Window {
    id: window
    width: 400
    height: 500
    visible: true
    title: "Login"
    flags: Qt.Window | Qt.FramelessWindowHint
    color: "transparent"

    // 添加一个属性来存储用户名和密码
    property string defaultUsername: "admin"
    property string defaultPassword: "123456"

    // 添加窗口阴影
    Rectangle {
        id: windowBorder
        anchors.fill: parent
        color: "white"
        radius: 10
        border.width: 1
        border.color: "#dddddd"

        // 窗口阴影效果
        layer.enabled: true
        layer.effect: DropShadow {
            transparentBorder: true
            horizontalOffset: 0
            verticalOffset: 2
            radius: 8.0
            samples: 17
            color: "#80000000"
        }

        // 标题栏区域（用于拖动窗口）
        Rectangle {
            id: titleBar
            height: 30
            color: "transparent"
            anchors {
                top: parent.top
                left: parent.left
                right: parent.right
            }

            // 关闭按钮
            Button {
                id: closeButton
                width: 30
                height: 30
                anchors.right: parent.right
                anchors.top: parent.top
                flat: true
                
                contentItem: Text {
                    text: "×"
                    color: closeButton.hovered ? "#FF0000" : "#666666"
                    font.pixelSize: 20
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }

                background: Rectangle {
                    color: "transparent"
                }

                onClicked: window.close()
            }

            // 使窗口可拖动
            MouseArea {
                anchors.fill: parent
                anchors.right: closeButton.left
                property point clickPos: "0,0"
                
                onPressed: {
                    clickPos = Qt.point(mouse.x, mouse.y)
                }
                
                onPositionChanged: {
                    var delta = Qt.point(mouse.x - clickPos.x, mouse.y - clickPos.y)
                    window.x += delta.x
                    window.y += delta.y
                }
            }
        }

        // 主要内容区域
        Rectangle {
            anchors.centerIn: parent
            width: 300
            height: 400
            color: "transparent"

            ColumnLayout {
                anchors.centerIn: parent
                spacing: 20
                width: parent.width - 40

                // Login text
                Text {
                    Layout.alignment: Qt.AlignHCenter
                    text: "LOGIN"
                    font.pixelSize: 24
                    font.bold: true
                    color: "#333333"
                }

                // Username field
                TextField {
                    id: usernameField
                    Layout.fillWidth: true
                    placeholderText: "用户名"
                    text: ""
                    background: Rectangle {
                        color: "#f5f5f5"
                        radius: 5
                        implicitHeight: 40
                    }
                }

                // Password field
                TextField {
                    id: passwordField
                    Layout.fillWidth: true
                    placeholderText: "密码"
                    text: ""
                    echoMode: TextField.Password
                    background: Rectangle {
                        color: "#f5f5f5"
                        radius: 5
                        implicitHeight: 40
                    }
                }

                // Error message
                Text {
                    id: errorMessage
                    Layout.fillWidth: true
                    color: "#FF0000"
                    font.pixelSize: 12
                    horizontalAlignment: Text.AlignHCenter
                    visible: false
                }

                // Login button
                Button {
                    Layout.fillWidth: true
                    text: "登录"
                    height: 40
                    
                    background: Rectangle {
                        color: parent.pressed ? "#0077cc" : "#0088ee"
                        radius: 5
                    }
                    
                    contentItem: Text {
                        text: parent.text
                        font.pixelSize: 16
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }

                    onClicked: {
                        if (usernameField.text === defaultUsername && 
                            passwordField.text === defaultPassword) {
                            // 创建并显示主窗口
                            var component = Qt.createComponent("MainWindow.qml")
                            if (component.status === Component.Ready) {
                                var mainWindow = component.createObject(null)
                                mainWindow.show()
                                window.close()
                            }
                        } else {
                            errorMessage.text = "用户名或密码错误"
                            errorMessage.visible = true
                        }
                    }
                }
            }
        }
    }
} 